const path = require('path')
const utils = require('./utils')
const config = require('../env_config')
const vueLoaderConfig = require('./vue-loader.conf')
const bootstrap = require('bootstrap-styl')
const stylusLoader = require('stylus-loader')

function resolve (dir) {
  return path.join(config.rootDirPath, dir)
}

module.exports = {
  entry: {
    app: './app/app.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json', '.styl'],
    modules: [
      resolve('app'),
      resolve('node_modules')
    ],
    alias: {
      'vue$': 'vue/dist/vue.common.js',
      'app': resolve('app'),
      'utils': resolve('app/utils_ide'),
      'assets': resolve('app/assets'),
      'components': resolve('app/components')
    }
  },
  module: {
    rules: [
      {
        test: /\.(js|vue)$/,
        loader: 'eslint-loader',
        enforce: 'pre',
        include: [resolve('app'), resolve('test')],
        options: {
          formatter: require('eslint-friendly-formatter')
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('app'), resolve('test')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        query: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  plugins: [
    new (stylusLoader.OptionsPlugin)({
      default: { use: [bootstrap()] }
    })
  ]
}
